package com.dotflat.xj.service.impl;

import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.dotflat.util.AssertUtil;
import com.dotflat.util.TargetUtil;
import com.dotflat.xj.domain.*;
import com.dotflat.xj.po.*;
import com.dotflat.xj.repository.*;
import com.dotflat.xj.service.TargetRecordService;
import com.dotflat.xj.service.TargetService;
import com.dotflat.util.UuidUtil;
import com.google.common.collect.ImmutableMap;
import com.matt.commons.dto.sso.AreaDTO;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.CopyUtils;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.service.TeachOperationLogService;
import com.matt.service.remoteCall.SsoCallService;
import com.tuo51.foudation.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class TargetRecordServiceImpl extends AreableObjectBaseServiceImpl<TargetRecord, TargetRecordRepository> implements TargetRecordService {

    @Autowired
    @Override
    public void setBaseRepository(TargetRecordRepository repository) {
        super.setBaseRepository(repository);
    }

    @Autowired
    protected MemVerificationCache cache;

    @Autowired
    private TargetProjectValueRepository targetProjectValueRepository;

    @Autowired
    private TargetRecordRepository targetRecordRepository;

    @Autowired
    private TargetValueRecordRepository targetValueRecordRepository;

    @Autowired
    private TargetRepository targetRepository;

    @Autowired
    private TargetService targetService;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private DataRepository dataRepository;

    @Autowired
    private BaseNativeSqlRepository baseNativeSqlRepository;

    @Autowired
    private TargetClassRepository targetClassRepository;

    @Autowired
    private TeachOperationLogService teachOperationLogService;

    @Autowired
    private TargetRecordV2Repository targetRecordV2Repository;

    @Override
    public Map details(String ticket, String targetRecordId) {
        StaffDTO staff = cache.getStaff(ticket);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        TargetRecord targetRecord = targetRecordRepository.findAllById(targetRecordId);
        if (AssertUtil.isEmpty(targetRecord)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        Target target = targetRepository.getOne(targetRecord.getTargetId());
        List<TargetValueRecord> list = targetValueRecordRepository.getRecordIdList(staff.getAreaId(), targetRecord.getTargetId(), targetRecord.getId(), targetRecord.getTime() + " 00:00:00");
        if (Objects.nonNull(targetRecord.getTargetClass())) {
            TargetClass targetClass = targetClassRepository.getOne(targetRecord.getTargetClass());
            targetRecord.setTargetClass(targetClass.getName());
        }

        //获取统计信息
        Map m = getCountMap(target, targetRecord);
        m.put("list", "");
        if (null != list && list.size() > 0) {
            List<Data> dataList = dataRepository.findAllByTypeAndVal("xj_conclusion_type",target.getConclusionType());
            List<Data> dataList1 = dataRepository.findAllByType(dataList.get(0).getLabel());
            List<TargetValueRecordNameListPO> ret = list.stream().map(o->{
                TargetValueRecordNameListPO po = new TargetValueRecordNameListPO();
                CopyUtils.copyProperties(o,po);
                po.setRecordSortName(TargetUtil.getResultName(dataList1,"",o.getRecordSortValue()));
                return po;
            }).collect(Collectors.toList());
            m.put("list", ret);
        }

        return m;
    }

    @Override
    @Transactional
    public void add(String ticket, TargetRecordPO targetRecord) {
        StaffDTO staff = cache.getStaff(ticket);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        Target target = targetRepository.findAllById(targetRecord.getTargetId());
        if (AssertUtil.isEmpty(target)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        TargetRecord targetRecord1 = targetRecordRepository.selectTargetRecord(staff.getAreaId(), targetRecord.getTargetId(), targetRecord.getTime(), targetRecord.getTargetClass(), staff.getId());
        if (AssertUtil.isEmpty(targetRecord1)) {
            targetRecord1 = new TargetRecord();
            targetRecord1.setId(UuidUtil.getUuid());
            targetRecord1.setAreaId(staff.getAreaId());
            targetRecord1.setCreatedBy(staff.getId());
            targetRecord1.setUserName(staff.getRealName());
        }
        CopyUtils.copyProperties(targetRecord, targetRecord1);
        targetRecord1 = targetRecordRepository.save(targetRecord1);

        List<TargetValueRecordPO> list = targetRecord.getList();
        int recordCount = 0;
        int recordNumber = 0;
        targetValueRecordRepository.deleteAllByRecordIdTime(targetRecord1.getId(), targetRecord.getTime() + " 00:00:00");
        if (null != list && list.size() > 0) {
            if (StringUtils.equals(target.getWhole(), "0")) {
                if (target.getProjectNumber() != list.size()) {
                    throw new BusinessException(ErrorCode.UNDEFINED, "不允许部分提交！");
                }
            }
            for (int i = 0; i < list.size(); i++) {
                TargetValueRecordPO targetValueRecordPO = list.get(i);
                TargetValueRecord targetValueRecord = new TargetValueRecord();
                //总分值
                recordCount += TargetUtil.getFractionValue(target.getConclusionType(), targetValueRecordPO.getRecordSortValue());
                //总检查数
                recordNumber += 1;

                CopyUtils.copyProperties(targetValueRecordPO, targetValueRecord);
                targetValueRecord.setTargetId(target.getId());
                targetValueRecord.setCreated(targetRecord1.getCreated());
                targetValueRecord.setAreaId(staff.getAreaId());
                targetValueRecord.setCreatedBy(staff.getId());
                targetValueRecord.setRecordId(targetRecord1.getId());
                targetValueRecordRepository.save(targetValueRecord);
            }
        }
        targetRecord1.setAreaId(staff.getAreaId());
        targetRecord1.setCreatedBy(staff.getId());
        targetRecord1.setUserName(staff.getRealName());
        targetRecord1.setRecordCount(recordCount);
        targetRecord1.setRecordNumber(recordNumber);
        targetRecordRepository.save(targetRecord1);

        teachOperationLogService.addTargetRecordLog(staff, target.getTypeName());
    }


    @Override
    public List<Map> census(String ticket, String targetId, String time) {
        StaffDTO staff = cache.getStaff(ticket);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        Target target = targetRepository.getOne(targetId);
        if (AssertUtil.isEmpty(target)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        if (null == time) {
            time = DateUtil.formatDateToStr(new Date());
        } else {
            time = time + " 00:00:00";
        }

        List<TargetRecord> list = targetRecordRepository.getList(staff.getAreaId(), target.getId(), time);
        List<Map> mapList = new ArrayList<>();
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                TargetRecord targetRecord = list.get(i);
                //获取统计信息
                Map m = getCountMap(target, targetRecord);
                mapList.add(m);
            }
        }
        return mapList;
    }


    public Map getCountMap(Target target, TargetRecord targetRecord) {
        Map m = new HashMap();
        /** 记录总分数 */
        m.put("recordCount", targetRecord.getRecordCount());
        /** 记录总检查项 */
        m.put("recordNumber", targetRecord.getRecordNumber());
        /** 总分数 */
        m.put("projectCount", target.getProjectCount());
        /** 总检查项 */
        m.put("projectNumber", target.getProjectNumber());
        /** 总检查项 */
        m.put("projectNumber", target.getProjectNumber());

        //班级信息
        m.put("targetClass", targetRecord.getTargetClass());
        //检查人
        m.put("userName", targetRecord.getUserName());
        //检查时间
        m.put("time", targetRecord.getCreated());

        m.put("targetRecordId", targetRecord.getId());

        Double count = Double.valueOf((Double.valueOf(targetRecord.getRecordCount()) / Double.valueOf(target.getProjectCount())) * 100);
        String percentage = "100%";
        if (count >= 0) {
            percentage = String.format("%.1f", count) + "%";
        }
        //百分比
        m.put("percentage", percentage);
        return m;
    }


    @Override
    public List<Map> groupCount(String ticket, int type, Boolean week, String time, String sort) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = new Date();
        if (null != time) {
            try {
                time1 = sdf.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<AreaDTO> list = ssoCallService.getStaffGroupByAreaList(ticket);
        List<Map> maps = new ArrayList<>();
        if (type == 0) {
            if (AssertUtil.isNotEmpty(list)) {
                for (int i = 0; i <= 6; i++) {
                    List<Map> maps1 = new ArrayList<>();
                    Map map = new HashMap();

                    for (int j = 0; j < list.size(); j++) {
                        Map map1 = new HashMap();
                        map1.put("areaName", list.get(j).getName());
                        Integer targetCount = 0;
                        if (week) {

                            targetCount = targetRecordRepository.weekCountSum(list.get(j).getId(), DateUtil.getReduceWeek((~(i - 1)), time1), DateUtil.getReduceWeekEnd((~(i - 1)), time1), sort);
                            map.put("category", DateUtil.getReduceWeek((~(i - 1)), time1).substring(0, 10) + " - " + DateUtil.getReduceWeekEnd((~(i - 1)), time1).substring(0, 10));
                        } else {
                            targetCount = targetRecordRepository.targetCountSum(list.get(j).getId(), DateUtil.getReduceDay((~(i - 1)), time1), sort);
                            map.put("category", DateUtil.getReduceDay((~(i - 1)), time1).substring(0, 10));
                        }
                        map1.put("targetCount", targetCount);
                        maps1.add(map1);
                    }
                    map.put("list", maps1);
                    maps.add(map);
                }
            }
        }
        if (type == 1) {
            List<Data> list1 = dataRepository.findAllByType("xj_sort");
            for (int i = 0; i < list1.size(); i++) {
                List<Map> maps1 = new ArrayList<>();
                Map map = new HashMap();
                map.put("category", list1.get(i).getVal());
                for (int j = 0; j < list.size(); j++) {
                    Map map1 = new HashMap();
                    map1.put("areaName", list.get(j).getName());
                    Integer targetCount = 0;
                    if (week) {
                        targetCount = targetRecordRepository.weekCountSum(list.get(j).getId(), DateUtil.getReduceWeek((~(i - 1)), time1), DateUtil.getReduceWeekEnd((~(i - 1)), time1), list1.get(i).getVal());
                    } else {
                        targetCount = targetRecordRepository.targetCountSum(list.get(j).getId(), DateUtil.getReduceDay(0, time1), list1.get(i).getVal());
                    }
                    map1.put("targetCount", targetCount);
                    maps1.add(map1);
                }
                map.put("list", maps1);
                maps.add(map);
            }
        }
        return maps;
    }


    @Override
    public List<Map> totalCount(String ticket, String startTime, String endTime, String sort, String areaId) {
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        List<Map> mapList = new ArrayList<>();
        if (!Objects.nonNull(areaId)) {
            List<AreaDTO> list = ssoCallService.getStaffGroupByAreaList(ticket);
            //总天数
            List<String> stringList = DateUtil.getBetweenDate(startTime, endTime);
            for (int i = 0; i < list.size(); i++) {
                List<Object[]> objects = targetRecordV2Repository.totalCount(list.get(i).getId(), startTime, endTime, sort);
                List<Target> targetList = targetRepository.getAllAreaId(list.get(i).getId());
                for (int j = 0; j < stringList.size(); j++) {
                    for (int k = 0; k < targetList.size(); k++) {
                        Map map = new HashMap();
                        map.put("targetType", targetList.get(k).getTargetType());
                        map.put("recordNumber", 0);
                        map.put("projectCount", 0);
                        map.put("recordCount", 0);
                        map.put("created", stringList.get(j));
                        map.put("areaId", list.get(i).getId());
                        map.put("areaName", list.get(i).getName());
                        map.put("average", 0);
                        for (int l = 0; l < objects.size(); l++) {
                            if (StringUtils.equals(stringList.get(j), objects.get(l)[4].toString()) &&
                                    StringUtils.equals(targetList.get(k).getId(), objects.get(l)[5].toString())) {
                                map.put("recordNumber", Integer.valueOf(objects.get(l)[1].toString()));
                                map.put("projectCount", Integer.valueOf(objects.get(l)[2].toString()));
                                map.put("recordCount", Integer.valueOf(objects.get(l)[3].toString()));
                                map.put("average", (Integer.valueOf(objects.get(l)[2].toString()) + Integer.valueOf(objects.get(l)[3].toString())) / 2);
                            }
                        }
                        mapList.add(map);
                    }

                }

            }
        } else {
            AreaDTO areaDTO = ssoCallService.getAreaDTO(ticket);
            //总天数
            List<String> stringList = DateUtil.getBetweenDate(startTime, endTime);
            List<Object[]> objects = targetRecordV2Repository.totalCount(areaId, startTime, endTime, sort);
            List<Target> targetList = targetRepository.getAllAreaId(areaDTO.getId());
            for (int j = 0; j < stringList.size(); j++) {
                for (int k = 0; k < targetList.size(); k++) {
                    Map map = new HashMap();
                    map.put("targetType", targetList.get(k).getTargetType());
                    map.put("recordNumber", 0);
                    map.put("projectCount", 0);
                    map.put("recordCount", 0);
                    map.put("created", stringList.get(j));
                    map.put("areaId", areaDTO.getId());
                    map.put("areaName", areaDTO.getName());
                    map.put("average", 0);
                    for (int l = 0; l < objects.size(); l++) {
                        if (StringUtils.equals(stringList.get(j), objects.get(l)[4].toString()) &&
                                StringUtils.equals(targetList.get(k).getId(), objects.get(l)[5].toString())) {
                            map.put("recordNumber", Integer.valueOf(objects.get(l)[1].toString()));
                            map.put("projectCount", Integer.valueOf(objects.get(l)[2].toString()));
                            map.put("recordCount", Integer.valueOf(objects.get(l)[3].toString()));
                            map.put("average", (Integer.valueOf(objects.get(l)[2].toString()) + Integer.valueOf(objects.get(l)[3].toString())) / 2);
                        }
                    }
                    mapList.add(map);
                }
            }
        }
        return mapList;
    }

    @Override
    public List<Map> groupTotalCount(String ticket, String startTime, String endTime) {
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        List<AreaDTO> list = ssoCallService.getStaffGroupByAreaList(ticket);
        List<Map> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            List<Object[]> objects = targetRecordV2Repository.groupTotalCount(list.get(i).getId(), startTime, endTime);
            for (int j = 0; j < objects.size(); j++) {
                Map map = new HashMap();
                map.put("recordNumber", Integer.valueOf(objects.get(j)[0].toString()));
                map.put("projectCount", Integer.valueOf(objects.get(j)[1].toString()));
                map.put("recordCount", Integer.valueOf(objects.get(j)[2].toString()));
                map.put("created", objects.get(j)[3].toString());
                map.put("average", (Integer.valueOf(objects.get(j)[1].toString()) + Integer.valueOf(objects.get(j)[2].toString())) / 2);
                map.put("areaName", list.get(i).getName());
                mapList.add(map);
            }
        }
        return mapList;
    }


    @Override
    public List<Map> countResult(String ticket, String startTime, String endTime, String type, String areaId) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long h = DateUtil.betweenHour(startTime + " 00:00:00", endTime + " 23:59:59");
        int day = (int) (Math.ceil(h / 24));
        List<Map> mapList = new ArrayList<>();
        for (int v = 0; v < day; v++) {
            if (!Objects.nonNull(areaId)) {
                List<AreaDTO> list = ssoCallService.getStaffGroupByAreaList(ticket);
                for (int i = 0; i < list.size(); i++) {
                    List<Object[]> objects = targetRecordV2Repository.countResult(list.get(i).getId(), DateUtil.getReduceDay(v, sdf.parse(startTime + " 00:00:00")), DateUtil.getReduceDay(v, sdf.parse(endTime + " 23:59:59")), type);
                    for (int j = 0; j < objects.size(); j++) {
                        Map map = new HashMap();
                        map.put("targetType", objects.get(j)[0].toString());
                        map.put("recordNumber", Integer.valueOf(objects.get(j)[1].toString()));
                        map.put("recordCount", Integer.valueOf(objects.get(j)[2].toString()));
                        map.put("projectCount", Integer.valueOf(objects.get(j)[3].toString()));
                        map.put("projectNumber", Integer.valueOf(objects.get(j)[4].toString()));
                        map.put("created", objects.get(j)[5].toString());
                        map.put("userName", objects.get(j)[6].toString());
                        map.put("time", objects.get(j)[7].toString());
                        map.put("recordId", objects.get(j)[8].toString());
                        map.put("areaName", list.get(i).getName());
                        Double count = Double.valueOf((Double.valueOf(objects.get(j)[2].toString()) / Double.valueOf(objects.get(j)[3].toString())) * 100);
                        String percentage = "100%";
                        if (count >= 0) {
                            percentage = String.format("%.2f", count) + "%";
                        }
                        //百分比
                        map.put("percentage", percentage);
                        mapList.add(map);
                    }
                }

            } else {
                AreaDTO areaDTO = ssoCallService.getAreaDTO(ticket);
                List<Object[]> objects = targetRecordV2Repository.countResultArea(areaId, DateUtil.getReduceDay(v, sdf.parse(startTime + " 00:00:00")), DateUtil.getReduceDay(v, sdf.parse(endTime + " 23:59:59")), type);
                for (int j = 0; j < objects.size(); j++) {
                    Map map = new HashMap();
                    map.put("targetType", objects.get(j)[0].toString());
                    map.put("recordNumber", Integer.valueOf(objects.get(j)[1].toString()));
                    map.put("recordCount", Integer.valueOf(objects.get(j)[2].toString()));
                    map.put("projectCount", Integer.valueOf(objects.get(j)[3].toString()));
                    map.put("projectNumber", Integer.valueOf(objects.get(j)[4].toString()));
                    map.put("created", objects.get(j)[5].toString());
                    map.put("userName", objects.get(j)[6].toString());
                    map.put("time", objects.get(j)[7].toString());
                    map.put("recordId", objects.get(j)[8].toString());
                    map.put("areaName", areaDTO.getName());
                    Double count = Double.valueOf((Double.valueOf(objects.get(j)[2].toString()) / Double.valueOf(objects.get(j)[3].toString())) * 100);
                    String percentage = "100%";
                    if (count >= 0) {
                        percentage = String.format("%.2f", count) + "%";
                    }
                    //百分比
                    map.put("percentage", percentage);
                    mapList.add(map);
                }
            }
        }
        return mapList;
    }

    @Override
    public List<Map> countResultDetails(String ticket, String recordId) {
        StaffDTO staff = cache.getStaff(ticket);
        if (AssertUtil.isEmpty(staff)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        List<Object[]> objects = targetRecordV2Repository.countResultDetails(recordId);
        List<Data> list = dataRepository.findAllByTypeAndVal("xj_conclusion_type", objects.get(0)[0].toString());
        List<Data> list1 = dataRepository.findAllByType(list.get(0).getLabel());
        List<Map> mapList = new ArrayList<>();
        for (int i = 0; i < objects.size(); i++) {
            Map map = new HashMap();
//            map.put("conclusionType", objects.get(i)[0].toString());
            map.put("sortName", objects.get(i)[1].toString());
            map.put("recordSortValue", objects.get(i)[2].toString());
            map.put("resultName", TargetUtil.getResultName(list1, objects.get(i)[0].toString(), objects.get(i)[2].toString()));
            mapList.add(map);
        }
        return mapList;
    }


    @Override
    public List<TargetTypeRecordPO> areaTypeCount(String ticket, String areaId, String startTime, String endTime) {

        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        List<AreaDTO> areaList = ssoCallService.getStaffGroupByAreaList(ticket);
        AreaDTO areaDTO = null;
        for (int i = 0; i < areaList.size(); i++) {
            if (StringUtils.equals(areaId, areaList.get(i).getId())) {
                areaDTO = areaList.get(i);
            }
        }
        if (Objects.isNull(areaDTO)) {
            throw new BusinessException(ErrorCode.IS_TARGET, "园区不存在！");
        }
        String groupId = ssoCallService.getAreaGroupId(ticket);
        //获取园区大类别
        List<Target> targetList = targetRepository.getAreaSort(areaDTO.getId(),groupId);
        List<Object[]> objects = targetRecordV2Repository.areaTypeCount(areaId, startTime, endTime);
        List<TargetTypeRecordPO> targetTypeRecordList = new ArrayList<>();
        for (int i = 0; i < targetList.size(); i++) {
            TargetTypeRecordPO po = new TargetTypeRecordPO();
            po.setAreaId(areaDTO.getId());
            po.setAreaName(areaDTO.getName());
            po.setTargetType(targetList.get(i).getSort());
            for (int j = 0; j < objects.size(); j++) {
                if (StringUtils.equals(targetList.get(i).getSort(), objects.get(j)[0].toString())) {
                    po.setTargetCount(Integer.valueOf(objects.get(j)[1].toString()));
                    po.setLastDate(objects.get(j)[4].toString());
                    po.setHighValue(Double.valueOf(objects.get(j)[5].toString()));
                    po.setLowValue(Double.valueOf(objects.get(j)[6].toString()));
                    po.setAverageValue(Double.valueOf(objects.get(j)[7].toString()));
                }
            }
            targetTypeRecordList.add(po);
        }
        return targetTypeRecordList;
    }

    @Override
    public List<TargetTypeRecordPO> typeNameCount(String ticket, String areaId, String startTime, String endTime) {
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        AreaDTO areaDTO = ssoCallService.getAreaDTO(ticket);
        if (null == areaDTO) {
            throw new BusinessException(ErrorCode.IS_TARGET, "园区不存在！");
        }
        String groupId = ssoCallService.getAreaGroupId(ticket);
        //获取园区大类别.
        List<Target> targetList = targetRepository.getAreaTypeName(areaDTO.getId(),groupId);

        List<Object[]> objects = targetRecordV2Repository.typeNameCount(ticket, areaId, startTime, endTime);
        List<TargetTypeRecordPO> targetTypeRecordList = new ArrayList<>();
        for (int i = 0; i < targetList.size(); i++) {
            TargetTypeRecordPO po = new TargetTypeRecordPO();
            po.setAreaId(areaDTO.getId());
            po.setAreaName(areaDTO.getName());
            po.setTargetType(targetList.get(i).getTypeName());
            for (int j = 0; j < objects.size(); j++) {
                if (StringUtils.equals(targetList.get(i).getTypeName(), objects.get(j)[0].toString())) {
                    po.setTargetCount(Integer.valueOf(objects.get(j)[1].toString()));
                    po.setLastDate(objects.get(j)[4].toString());
                    po.setHighValue(Double.valueOf(objects.get(j)[5].toString()));
                    po.setLowValue(Double.valueOf(objects.get(j)[6].toString()));
                    po.setAverageValue(Double.valueOf(objects.get(j)[7].toString()));
                }
            }
            targetTypeRecordList.add(po);
        }
        return targetTypeRecordList;
    }

    @Override
    public Page<TargetValueRecordAreaPO> getAreaCountResult(String areaId, String startTime, String endTime, String type, PagerQuery pagerQuery) {
        List<TargetValueRecordAreaPO> list = targetRecordV2Repository.getAreaCountResult(areaId, DateUtil.parseToDateV2(DateUtil.parseToDate(startTime), 0), DateUtil.parseToDateV2(DateUtil.parseToDate(endTime), 1), type, pagerQuery.getPageNum() * pagerQuery.getPageSize(), pagerQuery.getPageSize());
        Integer count = targetRecordV2Repository.getAreaCountResult(areaId, DateUtil.parseToDateV2(DateUtil.parseToDate(startTime), 0), DateUtil.parseToDateV2(DateUtil.parseToDate(endTime), 1), type);
        return new PageImpl<>(list, PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize()),count);
    }

    @Override
    public TargetTypeNamePO targetStatus(String areaId, String targetId,String targetClassId) {
        TargetTypeNamePO po = new TargetTypeNamePO();
        Target target = targetRepository.getOne(targetId);
        if (StringUtils.equals("日",target.getCycle())){
            Integer count = targetRecordV2Repository.getAreaCountResult(areaId, DateUtil.parseToDateV2(new Date(), 0), DateUtil.parseToDateV2(new Date(), 1), null,targetClassId,targetId);
            if (count>0){
                po.setStatus(1);
                po.setMsg("当日项目"+target.getTypeName()+"已巡检");
                return po;
            }
        }
        if (StringUtils.equals("周",target.getCycle())){
            Integer count = targetRecordV2Repository.getAreaCountResult(areaId, DateUtil.parseToDateV2(DateUtil.getBeginDayOfWeek(), 0), DateUtil.parseToDateV2(DateUtil.getEndDayOfWeek(), 1), null,targetClassId,targetId);
            if (count>0){
                po.setStatus(1);
                po.setMsg("本周项目"+target.getTypeName()+"已巡检");
                return po;
            }
        }

        if (StringUtils.equals("月",target.getCycle())){
            Integer count = targetRecordV2Repository.getAreaCountResult(areaId, DateUtil.parseToDateV2(DateUtil.getLastDayOfBeginMonth(), 0), DateUtil.parseToDateV2(DateUtil.getLastDayOfEndMonth(), 1), null,targetClassId,targetId);
            if (count>0){
                po.setStatus(1);
                po.setMsg("本月项目"+target.getTypeName()+"已巡检");
                return po;
            }
        }
        po.setStatus(0);
        po.setMsg("未巡检");
        return po;
    }


}